package com.wuercloud.controller;

import com.wuercloud.entity.AplicationEntity;
import com.wuercloud.service.AplicationService;
import com.wuercloud.utils.PageUtils;
import com.wuercloud.utils.R;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应用相关
 * 
 * @author ChinaSun
 * @email hjsw1@126.com
 * @date 2017/3/15 22:19
 */
@RestController
@RequestMapping("taplication")
@Api(value = "taplication", description = "应用相关操作")
public class AplicationController {
	@Autowired
	private AplicationService aplicationService;

	/**
	 * 列表
	 */

	@ApiOperation(value = "查询应用列表", notes = "返回应用列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@RequiresPermissions("taplication:list")
	public R list(@ApiParam(value = "应用名称", required = false) @RequestParam(required = false) String apliName,
			@ApiParam(value = "第几页", required = true) @RequestParam(required = false) Integer page,
			@ApiParam(value = "每页显示几条", required = true) @RequestParam(required = false) Integer limit) {
		Map<String, Object> map = new HashMap<>();
		map.put("apliName", apliName);
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);

		// 查询列表数据
		List<AplicationEntity> tAplicationList = aplicationService.queryList(map);
		int total = aplicationService.queryTotal(map);

		PageUtils pageUtil = new PageUtils(tAplicationList, total, limit, page);
      
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 应用列表
	 */
	@ApiOperation(value = "应用列表数据", notes = "返回应用列表数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	@RequiresPermissions("taplication:select")
	public R select() {
		// 查询列表数据
		List<AplicationEntity> list = aplicationService.queryList(new HashMap<String, Object>());

		return R.ok().put("list", list);
	}

	/**
	 * 信息
	 */
	@ApiOperation(value = "查询应用列表信息", notes = "返回根据id查询的应用列表信息数据", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/info/{apliId}", method = RequestMethod.GET)
	@RequiresPermissions("taplication:info")
	public R info(@ApiParam(value = "根据id查询获取对象", required = true) @PathVariable("apliId") Integer apliId) {
		AplicationEntity tAplication = aplicationService.queryObject(apliId);

		return R.ok().put("tAplication", tAplication);
	}

	/**
	 * 保存
	 */
	@ApiOperation(value = "保存应用数据", notes = "保存应用数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@RequiresPermissions("taplication:save")
	public R save(@RequestBody AplicationEntity tAplication) {
		aplicationService.save(tAplication);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@ApiOperation(value = "修改应用数据", notes = "修改应用数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@RequiresPermissions("taplication:update")
	public R update(@RequestBody AplicationEntity tAplication) {
		aplicationService.update(tAplication);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@ApiOperation(value = "删除应用数据", notes = "删除应用数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@RequiresPermissions("taplication:delete")
	public R delete(@RequestBody Integer[] apliIds) {
		aplicationService.deleteBatch(apliIds);

		return R.ok();
	}

}
